package com.conero.yangsu.controller.system;

import com.conero.yangsu.entity.FieldEntity;
import com.conero.yangsu.entity.ModuleEntity;
import com.conero.yangsu.lib.FeedbackData;
import com.conero.yangsu.mapper.FieldMapper;
import com.conero.yangsu.mapper.ModuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/system/field")
public class FieldController {
    @Autowired
    private FieldMapper fieldMapper;
    @Autowired
    private ModuleMapper moduleMapper;

    // 数据列获取
    @PostMapping("/getList")
    public FeedbackData getList(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        Page<FieldEntity> model = fieldMapper.findAll(PageRequest.of(current - 1, 20));
        HashMap<String, Object> result = new HashMap<>();
        List<FieldEntity> dataList = model.getContent();
        result.put("data", dataList);

        HashMap<String, Object> pagination = new HashMap<>();
        pagination.put("total", model.getNumber());
        pagination.put("current", current);
        result.put("pagination", pagination);
        return FeedbackData.success(result, "数据请求成功！");
    }

    //数据新增
    @PostMapping("/add")
    public FeedbackData add(FieldEntity fieldEntity) {
        fieldMapper.save(fieldEntity);
        return FeedbackData.success("数据新增成功！");
    }

    //数据新增
    @PostMapping("/upd")
    public FeedbackData upd(FieldEntity fieldEntity) {
        fieldMapper.save(fieldEntity);
        return FeedbackData.success("数据更新成功！");
    }

    //数据新增
    @PostMapping("/del")
    public FeedbackData del(FieldEntity fieldEntity) {
        if (fieldEntity.getId() == null) {
            return FeedbackData.error("数据删除失败，缺少必要参数！");
        }
        fieldMapper.deleteById(fieldEntity.getId());
        return FeedbackData.success("数据更新成功！");
    }

    //数据新增
    @PostMapping("/midSearch")
    public FeedbackData midSearch(@RequestParam(value = "search", defaultValue = "") String search,
                                  @RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "size", defaultValue = "20") Integer size) {
        // @todo 待实现数量限制
        // 最大数据（分页）
        if (size < 1) {
            size = 20;
        } else if (size > 50) {
            size = 50;
        }

        List<ModuleEntity> modules = moduleMapper.findByModuleLikeOrLabelLike("%" + search + "%", "%" + search + "%");
        HashMap<Object, Object> option = new HashMap<>();
        for (ModuleEntity module : modules) {
            option.put(module.getId(), module.getModule() + "-" + module.getLabel());
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("option", option);
        return FeedbackData.success(result);
    }
}
